﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using SilverlightApplication1.Controls;
using SilverlightApplication1.Models;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        private readonly BoxModel channel1 = new BoxModel();

        public MainPage()
        {
            InitializeComponent();
            Loaded += MainPage_Loaded;
            Application.Current.Host.Content.Resized += mainPage_Resized;
        }

        private void MainPage_Loaded(Object sender, RoutedEventArgs e)
        {
            BuildScheme();
            scaleGrid();
            animatedPlayer.toggleButton.Checked += delegate { channel1.RunAnimation(); };
            animatedPlayer.toggleButton.Unchecked += delegate { channel1.StopAnimation(); };
        }

        private void BuildScheme()
        {
           BlockModel blockKGL2Model = BlockModel.Create.Called("blockKGL2")
                                                        .WithDescriptionFromFile("blockKGL2.txt")
                                                        .WithText("КГ\nЛ1,Л2")
                                                        .WithGallery.Called("blockKGL2")
                                                        .AndCountOfPictures(4);
            blockKGL2.DataContext = blockKGL2Model;

            BlockModel blockUMKNA2Model = BlockModel.Create.Called("blockUMKNA2")
                                                           .WithDescriptionFromFile("blockUMKNA2.txt")
                                                           .WithText("УМН\nНА 2\nЛ3")
                                                           .WithGallery.Called("blockKGL2")
                                                           .AndCountOfPictures(4);
            blockUMKNA2.DataContext = blockUMKNA2Model;

            BlockModel blockIZGV09Model = BlockModel.Create.Called("blockIZGV09")
                                                            .WithDescriptionFromFile("blockIZGV09.txt")
                                                            .WithText("ИЗГВ09")
                                                            .WithGallery.Called("blockKGL2")
                                                            .AndCountOfPictures(4);
            blockIZGV09.DataContext = blockIZGV09Model;

            BlockModel blockIZGV17Model = BlockModel.Create.Called("blockIZGV17")
                                                           .WithDescriptionFromFile("blockIZGV17.txt")
                                                           .WithText("ИЗГВ17")
                                                           .WithGallery.Called("blockKGL2")
                                                           .AndCountOfPictures(4);
            blockIZGV17.DataContext = blockIZGV17Model;

            BlockModel blockFerritVentilModel = BlockModel.Create.Called("blockFerritVentil")
                                                                 .WithDescriptionFromFile("blockFerritVentil.txt")
                                                                 .WithText("Ферит.\nвентиль.")
                                                                 .WithGallery.Called("blockKGL2")
                                                                 .AndCountOfPictures(4);
            blockFerritVentil.DataContext = blockFerritVentilModel;

            BlockModel block194VV34Model = BlockModel.Create.Called("blockFerritVentil")
                                                            .WithDescriptionFromFile("block194VV34.txt")
                                                            .WithText("194ВВ34")
                                                            .WithGallery.Called("blockKGL2")
                                                            .AndCountOfPictures(4);

            block194VV34.DataContext = block194VV34Model;

            BlockModel block194GV01Model = BlockModel.Create.Called("block194GV01")
                                                            .WithDescriptionFromFile("block194GV01.txt")
                                                            .WithText("194ГВ01")
                                                            .WithGallery.Called("blockKGL2")
                                                            .AndCountOfPictures(4);


            block194GV01.DataContext = block194GV01Model;

            BlockModel block6FerritVentilModel = BlockModel.Create.Called("block6FerritVentil")
                                                                  .WithDescriptionFromFile("block6FerritVentil.txt")
                                                                  .WithText("Ферит.\nвентиль.")
                                                                  .WithGallery.Called("blockKGL2")
                                                                  .AndCountOfPictures(4);
            block6FerritVentil.DataContext = block6FerritVentilModel;

            BlockModel block194BB17Model = BlockModel.Create.Called("block194BB17")
                                                            .WithDescriptionFromFile("block194BB17.txt")
                                                            .WithText("194ВВ17")
                                                            .WithGallery.Called("blockKGL2")
                                                            .AndCountOfPictures(4);
            block194BB17.DataContext = block194BB17Model;

            BlockModel block194BB11Model = BlockModel.Create.Called("block194BB11")
                                                            .WithDescriptionFromFile("block194BB11.txt")
                                                            .WithText("194ГВ11")
                                                            .WithGallery.Called("blockKGL2")
                                                            .AndCountOfPictures(4);
            block194BB11.DataContext = block194BB11Model;

            BlockModel block194BB13Model = BlockModel.Create.Called("block194BB13")
                                                            .WithDescriptionFromFile("block194BB13.txt")
                                                            .WithText("194ВВ13")
                                                            .WithGallery.Called("blockKGL2")
                                                            .AndCountOfPictures(4);
            block194BB13.DataContext = block194BB13Model;

            BlockModel blockVU660054Model = BlockModel.Create.Called("blockVU660054")
                                                             .WithDescriptionFromFile("blockVU660054.txt")
                                                             .WithText("ВЮ2\n660 054\nПлата\nзапрета")
                                                             .WithGallery.Called("blockKGL2")
                                                             .AndCountOfPictures(4);
            blockVU660054.DataContext = blockVU660054Model;

            channel1.AddToScenario(motionPath14);
            channel1.AddToScenario(blockVU660054);
            channel1.AddToScenario(horizontalPath1);
            channel1.AddToScenario(verticalPath1);
            channel1.AddToScenario(motionPath0);
            channel1.AddToScenario(blockKGL2);
            channel1.AddToScenario(motionPath1);
            channel1.AddToScenario(blockUMKNA2);            
            channel1.AddToScenario(motionPath2);
            
            channel1.AddToScenario(blockIZGV09);
            channel1.AddToScenario(motionPath4);
            channel1.AddToScenario(blockIZGV17);
            channel1.AddToScenario(motionPath5);
            channel1.AddToScenario(blockFerritVentil);
            channel1.AddToScenario(motionPath6);
            channel1.AddToScenario(block194VV34);
            channel1.AddToScenario(motionPath7);

            channel1.AddToScenario(block194GV01);
            channel1.AddToScenario(motionPath9);
            channel1.AddToScenario(block6FerritVentil);
            channel1.AddToScenario(motionPath10);
            channel1.AddToScenario(block194BB17);
            channel1.AddToScenario(motionPath11);
            channel1.AddToScenario(block194BB11);
            channel1.AddToScenario(motionPath12);
            channel1.AddToScenario(block194BB13);
        }

        private void RunAnimation_Click(Object sender, RoutedEventArgs e)
        {
            channel1.RunAnimation();
        }

        private void StopAnimation_Click(object sender, RoutedEventArgs e)
        {
            channel1.StopAnimation();
        }

        private void scaleGrid()
        {
            LayoutRoot.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            var xTransform = Application.Current.MainWindow.Width / LayoutRoot.DesiredSize.Width;
            var yTransform = Application.Current.MainWindow.Height / LayoutRoot.DesiredSize.Height;
            LayoutRoot.RenderTransform = new ScaleTransform { ScaleX = xTransform, ScaleY = yTransform };
            MainMenu.RenderTransform = new ScaleTransform { ScaleX = 1 / xTransform, ScaleY = 1 / yTransform };
        }

        private void mainPage_Resized(object sender, EventArgs eventArgs)
        {
            scaleGrid();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}